var express = require('express');
var router = express.Router();
var Comment = require('../models/comment');
var Articles = require('../models/articles');

/* POST users listing. */
router.post('/', function(req, res, next) {
      // 获取用户提交的信息
    var postData = {
        articleId: req.body.articleId,
        from: req.session.sign.userId,
        to: req.body.to,
        content : req.body.content,
        c_id:req.body.c_id, //被评论的评论的id
    };
    console.log(postData)
    //保存文章信息，到数据库
        Comment.findById(postData.c_id, function (err,comment) {
            console.log("comment:"+comment)
            if(!comment) {
                Comment.create(postData, function (err, data) {
                    console.log(err)
                    if (err) {
                        res.send({
                            state:0,
                            msg:"保存失败"  
                        })  
                        throw err;
                    }else{
                        console.log('保存成功');
                        res.send({
                            state:1,
                            msg:"保存成功"  
                        })  
                    }
                })
            }else{
                console.log("postData.c_id: "+postData.c_id)
                if(postData.c_id){
                    var c_id = postData.c_id;
                    //如果是评论的评论
                        console.log('43~comment: ')
                        console.log(comment)
                        var reply ={
                            from : postData.from,
                            to : postData.to,
                            content: postData.content
                        }
                    comment.reply.push(reply)
                    comment.save(function(err,comment){
                        if(err) {
                            console.log(err);
                            res.send({
                                state:0,
                                msg:'评论失败'
                            })
                        }else{
                            res.send({
                                state:1,
                                msg:'评论成功'
                            })
                        }

                    })
                }else{
                    comment.save(postData,function(err,isMatch){
                        if(err) console.log(err)
                        if(isMatch){
                            res.send({
                                state:1,
                                msg:'评论成功'
                            })
                        }else{
                            res.send({
                                state:0,
                                msg:'评论失败'
                            })
                        }
                    })
                }
               
            }
            
        })
});

/* GET users listing. */
router.get('/', function(req, res, next) {
        var CommentList = Comment.find({})
        .populate("from","username")
        .populate("reply.from reply.to","username")
        .exec(function (err, data) {
        if (err) throw  err;
        res.send(data)
    });
})
module.exports = router;
